// import { ElMessage } from 'element-plus'
import axios from 'axios'
// import { useUserStore } from '@/store/modules/user'

const req = axios.create({
  baseURL: import.meta.env.VITE_APP_SERVICE_URL,
  timeout: 5000,
})

// const Logout = (userStore: any) => {
//   userStore.removeUser()
//   ElMessage({
//     message: '当前登录的用户权限被修改，请重新登录',
//     type: 'warning',
//   })
//   setTimeout(() => {
//     window.location.href = '/'
//   }, 1000)
// }

const getMenus = () => {
  // const userStore = useUserStore()
  const roleId = '123'
  const userMenus = []
  // if (userStore.user?.username == 'admin') return
  req({
    url: '/menus',
    method: 'post',
    data: {
      roleId,
    },
  }).then((rep) => {
    const res = rep
    if (res.status == 0) {
      const menus = res.data.menus
      if (userMenus?.length == menus.length) {
        userMenus.forEach((item) => {
          if (menus.indexOf(item) === -1) {
            // Logout(userStore)
          }
        })
      } else {
        // Logout(userStore)
      }
    }
  })
}

req.interceptors.request.use(
  function (config) {
    if (config.url != '/menus' && config.url != '/') getMenus()
    return config
  },
  function (error) {
    return Promise.reject(error)
  },
)
// 响应拦截器
req.interceptors.response.use(
  function (response) {
    return response.data
  },
)

export default req
